package com.xm.dao.background;

import com.xm.entity.Departments;
import com.xm.util.BaseUtil;
import org.intellij.lang.annotations.Language;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 职称表Dao
 *
 * @author 陈盼
 * @date 2023-01-05
 */
public class DepartmentsDao extends BaseUtil {
    /**
     * 查询全部职称信息
     *
     * @return 职称列表
     */
    public List<Departments> findAll() {
        // SQL语句
        @Language("SQL")
        String sql = "select * from departments";
        // 执行查询操作
        ResultSet rs = null;
        List<Departments> departmentsList = new ArrayList<>();
        try {
            rs = executeQuery(sql);
            // 遍历结果集
            while (rs.next()) {
                Departments departments=new Departments();
                departments.setDepartmentId(rs.getInt("department_id"));
                departments.setDepartmentName(rs.getString("department_name"));
                departmentsList.add(departments);
            }
        } catch (SQLException e) {
            throw new RuntimeException("sql语法错误",e);
        } finally {
            super.close(rs);
        }
        return departmentsList;
    }
}
